// 引入数据库
const conn = require('../data/gy_index');

// 显示我的知识页面
module.exports.lyj_knowledge = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    res.render('lyj_knowledge');
}

// 显示添加知识页面
module.exports.lyj_knowledge_add = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
     }
    res.render('lyj_knowledge_add');
}
module.exports.lyj_knowledge_show = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
     }
    res.render('lyj_knowledge_show')
}
// 添加数据
module.exports.writeAdd1 = (req, res) => {
    console.log(req.query.sortid);
    conn.query('insert into lyj_knowledge (name,title,label,url,intro,content) values(?,?,?,?,?,?)', [req.query.sortid, req.query.title, req.query.tag, req.query.url, req.query.summary, req.query.content], (err, result) => {
        if (err) {
            return console.log(err);
        }
        // console.log(result);

        res.json({
            code: '1201',
            message: '保存成功'
        })
    })
}


module.exports.showUser1 = (req, res) => {
    conn.query('select * from lyj_classify', (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json(result)
        }
    })
}

module.exports.showUser3 = (req, res) => {
    conn.query('select * from lyj_classify', (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json(result)
        }
    })
}

module.exports.showUser4 = (req, res) => {
    conn.query('select * from lyj_classify', (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json(result)
        }
    })
}

module.exports.showUser2 = (req, res) => {
    conn.query('select * from lyj_knowledge', (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json(result)
        }
    })
}

// 删除数据
module.exports.delUser2 = (req, res) => {
    conn.query('delete from lyj_knowledge where id= ' + req.query.id, (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json({
                code: '1301',
                message: '删除成功'
            })
        }
    })
}

// 修改
module.exports.userUpdate2 = (req, res) => {
    // console.log(params);
    conn.query('update lyj_knowledge set name=? where id=?', [req.body.title, req.body.id], (error, result) => {
        if (error) return console.log(error);

        //   console.log(results);
        if (result) {
            res.json({
                code: '1302',
                message: '更新成功'
            })
        }
    })
}

module.exports.lyj_knowledge_add2 = (req, res) => {
    // res.render('lyj_add_classify2');
    console.log(req.query.id);
    conn.query('select a.id,a.title,a.label,a.url,a.intro,a.content,b.name from lyj_knowledge as a , lyj_classify as b where a.name = b.name and a.id = ?', req.query.id, (err, result) => {
        if (err) {
            return console.log(err);
        }
        // console.log(result);
        res.render('lyj_knowledge_add2', result[0]);
    })
}
